package xuesm.singleton.register;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: xuesm
 * @Description: 容器登记式单例
 * @Date: Create in 11:39 2019/3/30
 */
public class ContainerSingleton {

	private ContainerSingleton(){};

	private static Map<String, Object> SINGLETON_MAP = new ConcurrentHashMap<String, Object>();

	public static Object getInstance(String className){	// 工厂模式的感觉

		synchronized (SINGLETON_MAP){
			if (!SINGLETON_MAP.containsKey(className) ){
				Object object = null;
				try {
					object = Class.forName(className).newInstance();
					SINGLETON_MAP.put(className, object);
				}catch (Exception e){
					e.printStackTrace();
				}

				return object;
			}
		}


		return SINGLETON_MAP.get(className);
	}
}
